import 'package:flutter/material.dart';
import 'package:mall/entity/home_entity.dart';
import 'package:mall/page/home/new_product.dart';

class StickyDemo extends StatefulWidget {
  List<FloorGoodsList> floorGoodsList;

  @override
  _StickyDemoState createState() => _StickyDemoState(this.floorGoodsList);

  StickyDemo(this.floorGoodsList);
}

class _StickyDemoState extends State<StickyDemo>
    with SingleTickerProviderStateMixin {
  TabController tabController;
  List<FloorGoodsList> floorGoodsList;
  List<FloorGoodsList> notEmptyfloorGoodsList = List<FloorGoodsList>();

  _StickyDemoState(this.floorGoodsList);

  @override
  void initState() {
    super.initState();
    this.tabController = TabController(length: 2, vsync: this);
  }

  List<Widget> buildTabList(List<FloorGoodsList> floorGoodsList) {
    var widgetList = List<Widget>();
    floorGoodsList.forEach((element) {
      widgetList.add(Tab(
        text: element.name,
      ));
    });
    return widgetList;
  }

  List<Widget> buildTabView(List<FloorGoodsList> notEmptyfloorGoodsList) {
    var widgetList = List<Widget>();
    notEmptyfloorGoodsList.forEach((floorGoods) {
      widgetList.add(Container(
        child: Column(
          children: <Widget>[
            Container(
              alignment: Alignment.center,
            ),
            ProductView(floorGoods.goodsList),
          ],
        ),
      ));
    });
    return widgetList;
  }

  List<FloorGoodsList> removeEmptyGoodsList(
      List<FloorGoodsList> floorGoodsList) {
    floorGoodsList.forEach((element) {
      if (element.goodsList.length > 0) {
        this.notEmptyfloorGoodsList.add(element);
      }
    });
    return this.notEmptyfloorGoodsList;
  }

  @override
  Widget build(BuildContext context) {
//    removeEmptyGoodsList(floorGoodsList);
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
//          SliverAppBar(
//            pinned: true,
//            elevation: 0,
//            expandedHeight: 250,
//            flexibleSpace: FlexibleSpaceBar(
//              title: Text('Sliver-sticky效果'),
//              background: Image.network(
//                'http://img1.mukewang.com/5c18cf540001ac8206000338.jpg',
//                fit: BoxFit.cover,
//              ),
//            ),
//          ),
          SliverPersistentHeader(
            pinned: true,
            delegate: StickyTabBarDelegate(
              child: TabBar(
                labelColor: Colors.black,
                controller: this.tabController,
                tabs: <Widget>[
                  Tab(text: 'Home'),
                  Tab(text: 'Profile'),
                ],
              ),
            ),
          ),
          SliverFillRemaining(
            child: TabBarView(
              controller: this.tabController,
              children: <Widget>[
                Center(child: Text('Content of Home')),
                Center(child: Text('Content of Index')),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;

  StickyTabBarDelegate({@required this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return this.child;
  }

  @override
  double get maxExtent => this.child.preferredSize.height;

  @override
  double get minExtent => this.child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
